package com.example.hello.hello.base;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hello.hello.R;
import com.example.hello.hello.dialog.DialogHelp;
import com.example.hello.hello.dialog.Loading;


public abstract class BaseActivity extends FragmentActivity implements View.OnClickListener {
    protected LinearLayout title;
    protected ImageButton mliftButton;
    protected ImageButton mrightButton;
    protected TextView textView;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        _isVisible = true;
        if (getLayoutId() != 0) {
            setContentView(getLayoutId());
        }
        initView(savedInstanceState);

    }


    public void initTitleBar() {
        title = (LinearLayout) findViewById(R.id.l_title);
        if (title != null) {
            title.setVisibility(View.VISIBLE);
            textView = (TextView) findViewById(R.id.tv_title);
            mliftButton = (ImageButton) findViewById(R.id.ib_titleback);
            mrightButton = (ImageButton) findViewById(R.id.ib_mytitle);
            mliftButton.setOnClickListener(this);
            mrightButton.setOnClickListener(this);
        }
    }

    protected int getLayoutId() {
        return 0;
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_titleback:
                onClickLeftBtn();
                break;
            case R.id.ib_mytitle:
                onClickReghtBtn();
                break;
        }
    }

    /**
     * 设置中间文本
     */
    protected void setMidTxt(String strTxt) {
        if (textView != null) {
            textView.setText(strTxt);
        }
    }

    protected void setMidTxtbyInt(int strTxt) {
        if (textView != null) {
            textView.setText(strTxt);
        }
    }

    protected void setLeftVisibility(Boolean isVisibility) {
        if (isVisibility) {
            mliftButton.setVisibility(View.GONE);
        } else {
            mliftButton.setVisibility(View.VISIBLE);
        }

    }

    protected void setRightVisibility(Boolean isVisibility) {
        if (isVisibility) {
            mrightButton.setVisibility(View.GONE);
        } else {
            mrightButton.setVisibility(View.VISIBLE);
        }
    }

    protected void setRightImg(int img) {
        if (img != 0) {
            mrightButton.setBackgroundResource(img);
        }
    }

    protected void setRightImg(Drawable img) {
        if (img != null) {
            mrightButton.setImageDrawable(img);
        }
    }

    protected void showToast(String message) {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_text,null);
        TextView text = (TextView) layout.findViewById(R.id.tv_tomst_text);
        text.setText(message);
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();
    }

    protected abstract void onClickLeftBtn();

    protected abstract void onClickReghtBtn();

    /**
     * 初始化页面
     *
     * @param savedInstanceState
     */
    protected abstract void initView(Bundle savedInstanceState);

    protected boolean _isVisible;
    protected Loading _waitDialog;
    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    protected Loading showWaitDialog() {
        return showWaitDialog(R.string.loading);
    }

    protected Loading showWaitDialog(int resid) {
        return showWaitDialog(getString(resid));
    }

    protected Loading showWaitDialog(String message) {
        if (_isVisible) {
            if (_waitDialog == null) {
                _waitDialog = DialogHelp.getWaitDialog(BaseActivity.this,message);
            }
            if (_waitDialog != null) {
                _waitDialog.setMessage(message);
                _waitDialog.show();
            }
            return _waitDialog;
        }
        return null;
    }

    protected void hideWaitDialog() {
        if (_isVisible && _waitDialog != null) {
            try {
                _waitDialog.dismiss();
                _waitDialog = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

}
